package com.yiyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyang.constant.ResultCode;
import com.yiyang.domain.ResultPage;
import com.yiyang.domain.dto.BedDetailsDto;
import com.yiyang.domain.po.Bed;
import com.yiyang.domain.po.BedDetails;
import com.yiyang.domain.po.Customer;
import com.yiyang.domain.vo.BedDetailsVo;
import com.yiyang.exception.BusinessException;
import com.yiyang.mapper.BedMapper;
import com.yiyang.mapper.CustomerMapper;
import com.yiyang.mapper.RoomMapper;
import com.yiyang.service.BedDetailsService;
import com.yiyang.mapper.BedDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

/**
* @author itcast
* @description 针对表【bed_details】的数据库操作Service实现
* @createDate 2024-12-18 14:37:52
*/
@Service
public class BedDetailsServiceImpl extends ServiceImpl<BedDetailsMapper, BedDetails>
    implements BedDetailsService{

    @Autowired
    private BedDetailsMapper bedDetailsMapper;

    @Override
    public ResultPage<BedDetailsVo> listBedDetailsVoPage(Integer page, Integer pageSize, String customerName, LocalDateTime startDate, LocalDateTime endDate, Integer isDeleted) {
        // 封装分页查询条件
        Page<BedDetailsVo> bedDetailsPage = new Page<>(page, pageSize);
        bedDetailsMapper.selectBedDetailsPageByCondition(bedDetailsPage,customerName,startDate,endDate,isDeleted);

        return new ResultPage<BedDetailsVo>(bedDetailsPage.getTotal(),bedDetailsPage.getRecords());
    }

    @Override
    public void updateBedDetails(BedDetails bedDetails) {
        // 调用一个或者多个dao层方式,实现业务逻辑处理
        BedDetails newBedDetails = new BedDetails();

        newBedDetails.setId(bedDetails.getId());
        newBedDetails.setEndDate(bedDetails.getEndDate());

        bedDetailsMapper.updateById(newBedDetails);
    }

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exchangeBed(BedDetailsDto bedDetailsDto) {
        // 调用一个或者多个dao层方式,实现业务逻辑处理
        // 1.	只能调换到空闲的床位
        Bed newBed = bedMapper.selectById(bedDetailsDto.getNewBedId());
        if(newBed.getStatus() == 2){
            throw new BusinessException(ResultCode.BED_USED_CODE,"床位被占用,不能调换");
        }
        // 2.	删除客户旧床位详情信息(更改为失效)床位使用结束时间为当前日期
        BedDetails oldBedDetails = new BedDetails();
        oldBedDetails.setId(bedDetailsDto.getId());
        oldBedDetails.setIsDeleted(1);
        oldBedDetails.setEndDate(new Date());

        int row1 = bedDetailsMapper.updateById(oldBedDetails);// 0
        // 3.	添加新床位记录的记录，入住时间为当前日期
        BedDetails newBedDetails = new BedDetails();
        newBedDetails.setStartDate(new Date());
        newBedDetails.setEndDate(bedDetailsDto.getEndDate());
        newBedDetails.setBedDetails("606#"+newBed.getBedNo());
        newBedDetails.setCustomerId(bedDetailsDto.getCustomerId());
        newBedDetails.setBedId(bedDetailsDto.getNewBedId());

        int row2 = bedDetailsMapper.insert(newBedDetails);
        // 4.	修改旧床位的状态为空闲，修改新床位的状态为有人
        Bed oldBed = bedMapper.selectById(bedDetailsDto.getOldBedId());
        oldBed.setStatus(1);
        int row3 = bedMapper.updateById(oldBed);

        newBed.setStatus(2);
        int row4 = bedMapper.updateById(newBed);
        // 5.	修改客户信息包括：新房间号、 新床位号、楼号
        Customer customer = customerMapper.selectById(bedDetailsDto.getCustomerId());
        customer.setRoomNo(bedDetailsDto.getNewRoomNo());
        customer.setBedId(bedDetailsDto.getNewBedId());
        customer.setBuildingNo(bedDetailsDto.getBuildingNo());

        int row5 = customerMapper.updateById(customer);

        if(row1 == 0 || row2 == 0 || row3 == 0 ||row4 == 0 ||row5 == 0 ){
            throw new BusinessException(ResultCode.BED_EXCHANGE_CODE,"床位调换失败");
        }
    }
}




